//
//  OSCBaseTableViewModel.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/2.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCBaseTableViewModel.h"

@interface OSCBaseTableViewModel ()

@property (nonatomic, strong, readwrite) RACCommand *requestListDataCommand;

@end

@implementation OSCBaseTableViewModel
- (instancetype)initWithServices:(id<OSCViewModelServices>)services params:(NSDictionary *)params {
    self = [super initWithServices:services params:params];
    if (self) {
        _tableViewStyle = [params objectForKey:@"tableStyle"] ? [[params objectForKey:@"tableStyle"] integerValue] : 0;
        _autoRefreshStatus = YES;
    }
    return self;
}
- (void)initialize {
     @weakify(self);
    self.requestListDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        return [[self requestListDataSignal]
                takeUntil:self.rac_willDeallocSignal];
    }];
    [RACObserve(self, dataSourceSignal) subscribeNext:^(RACSignal *signal) {
        @strongify(self);
        [[signal takeUntil:self.dataSourceSignal.rac_willDeallocSignal]
         subscribeNext:^(NSArray *viewModels) {
            @strongify(self);
            if (self.listParams.pageIndex == 1) {
                self.dataSource = viewModels;
                if (self.autoRefreshStatus) {
                    [self makeHeaderRefreshWithArray:viewModels];
                }
            } else {
                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                if (self.autoRefreshStatus) {
                    [self makeFootRefreshWithArray:viewModels];
                }
            }
        }];
    }];
    [self.errors subscribeNext:^(NSError *error) {
        @strongify(self);
        [self.refreshEndSubject sendNext:@(OSCRefreshError)];
    }];
}
- (RACSignal *)dataSourceSignal {
    if (!_dataSourceSignal) {
        _dataSourceSignal = [RACSignal empty];
    }
    return _dataSourceSignal;
}
- (RACSignal *)requestListDataSignal {
    return [RACSignal empty];
}
- (OSCListRequestParams *)listParams {
    if (!_listParams) {
        _listParams = [OSCListRequestParams new];
    }
    return _listParams;
}
- (RACSubject *)refreshEndSubject {
    if (!_refreshEndSubject) {
        _refreshEndSubject = [RACSubject subject];
    }
    return _refreshEndSubject;
}
- (void)makeHeaderRefreshWithArray:(NSArray *)array {
    if (array.count < self.listParams.pageSize) {
        [self.refreshEndSubject sendNext:@(OSCHeaderRefresh_HasNoMoreData)];
    } else {
        [self.refreshEndSubject sendNext:@(OSCHeaderRefresh_HasMoreData)];
    }
}
- (void)makeFootRefreshWithArray:(NSArray *)array {
    if (array.count < self.listParams.pageSize) {
        [self.refreshEndSubject sendNext:@(OSCFooterRefresh_HasNoMoreData)];
    } else {
        [self.refreshEndSubject sendNext:@(OSCFooterRefresh_HasMoreData)];
    }
}
- (RACSubject *)needRefresh {
    if (!_needRefresh) {
        _needRefresh = [RACSubject subject];
    }
    return _needRefresh;
}
- (RACSubject *)refreshUI {
    if (!_refreshUI) {
        _refreshUI = [RACSubject subject];
    }
    return _refreshUI;
}
@end
